# coding=UTF-8
'''
Created on 15 Mar 2012

@author: R
'''

import re
from base import processpage, search_term
from tools.BeautifulSoup import BeautifulSoup


def get_urls_from_portal(mpage_url,proxy):
    mpage_text = processpage(mpage_url,proxy)
    portal = re.compile("fb_blue.*?href..(.*?htm)\">更多详细").search(mpage_text)
    page = search_term(portal)
    if page != 'N/A' and page.find('soufun') == -1:
        page = mpage_url.replace('.htm','/housedetail.htm')
    phpurl = re.compile("iframe_map.*?(http.*?)\"").search(mpage_text) 
    if page == 'N/A':
        return ("0","0")
    else:
        return (page,search_term(phpurl))
   
    
def resolve_phppage(url,proxy):
    content=processpage(url,proxy)
    idt=re.compile("newcode[^\d]+?(\d+)").search(content)
    city=re.compile("cityname[^\"]+\"(.*?)\"").search(content)
    cityx=re.compile("cityx.(.*?)\;").search(content)
    cityy=re.compile("cityy.(.*?)\;").search(content)
    price=re.compile("price_num[^\d]+?(\d+)").search(content)
    stri=""+search_term(idt)+","+search_term(city)+","+search_term(cityx)+","+search_term(cityy)+","+search_term(price)+","
    return stri


def resolve_detailpage(url,proxy):
    content=processpage(url,proxy)
    soup=BeautifulSoup(content)
    
    #top cubic
    ss=str(soup.findAll("td")).split()
    sw="".join(ss)
    wuyelb=re.compile("物业类别[^>]+?>(.*?)<").search(sw)
    xiangmuts=re.compile("项目特色[^>]+?>(.*?)<").search(sw)
    jianzhulb=re.compile("建筑类别[^>]+?>(.*?)<").search(sw)
    zhuangxiuzk=re.compile("装修状况[^>]+?>(.*?)<").search(sw)
    rongjil=re.compile("容积率[^>]+?>(.*?)<").search(sw)
    lvhual=re.compile("绿化率[^>]+?>(.*?)<").search(sw)
    kaipansj=re.compile("开盘时间[^>]+?>(.*?)<").search(sw)
    ruzhusj=re.compile("入住时间[^>]+?>(.*?)<").search(sw)
    wuyef=re.compile("物业费[^>]+?>(.*?)<").search(sw)
    wuyegs=re.compile("物业公司[^>]+?>[^>]+?>[^>]+?>(.*?)<").search(sw)
    kaifas=re.compile("开发商[^>]+?>[^>]+?>[^>]+?>(.*?)<").search(sw)
    yushouxkz=re.compile(">预售许可证[^>]+?>(.*?)<").search(sw)
    shouloudz=re.compile("售楼地址[^>]+?>(.*?)<").search(sw)
    wuyedz=re.compile("物业地址[^>]+?>(.*?)<").search(sw)
    stri=""+search_term(wuyelb)+","+"".join(search_term(xiangmuts).split(","))+","+"".join(search_term(jianzhulb).split(","))+","+"".join(search_term(zhuangxiuzk).split(","))+","+search_term(rongjil)+","+search_term(lvhual)+","+search_term(kaipansj)+","+search_term(ruzhusj)+","+search_term(wuyef)+","+search_term(wuyegs)+","+search_term(kaifas)+","+search_term(yushouxkz)+","+search_term(shouloudz)+","+search_term(wuyedz)+","
    
    #other cubic
    ss=str(soup.findAll(attrs={"class":"lineheight"})).split()
    sw="".join(ss)
    zhandimj=re.compile("占地面积[^>]+?>(.*?)<").search(sw)
    jianzhumj=re.compile("建筑面积[^>]+?>(.*?)<").search(sw)
    kaigongsj=re.compile("开工时间[^>]+?>(.*?)<").search(sw)
    chanquannx=re.compile("产权年限[^>]+?>(.*?)<").search(sw)
    hushu=re.compile("户数[^>]+?>(.*?)<").search(sw)
    stri=stri+search_term(zhandimj)+","+search_term(jianzhumj)+","+search_term(kaigongsj)+","+search_term(chanquannx)+","+search_term(hushu)+","
    peitao=re.compile("lineheight(.*?)lineheight").findall(sw)
    for any in peitao:
        stri=stri+any+","
    return stri


'''
house/1010723183/fangjia.htm 价格走势
house/1010723183/sale_history.htm 开盘详情
'''

def run():
    infile = "..\\lists.txt"
    outfile = "..\\estate.csv"
    fh_getl = open(infile,"r")
    fh_putd = open(outfile,"a")
    proxy="0"
    for lines_in_links in fh_getl:
        strg = re.compile("^(.*?),").search(lines_in_links).group(1)+","
        url_m = re.compile(",(.*?)$").search(lines_in_links).group(1)
        (url_d, url_php) = get_urls_from_portal(url_m,proxy)
        if url_d != "0" and url_php != "0":
            strg = strg + url_d+","
            strg = strg + resolve_phppage(url_php,proxy)
            strg = strg + resolve_detailpage(url_d,proxy)
        else:
            strg = strg + url_m
        fh_putd.write(strg+"\n")
        print "done"+url_d


if __name__ == '__main__':
    run()